package org.ligson.user.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class BroastMessageListener {
    //1、定义fanout类型的交换器
    @Bean("smsExchange")
    public FanoutExchange smsExchange() {
        return ExchangeBuilder.fanoutExchange("smsExchange").build();
    }

    @Bean(name = "smsQueue")
    public Queue smsQueue() {
        return new Queue("smsQueue");
    }

    //3、将两个不同名称的消息队列跟交换器进行绑定
    @Bean
    public Binding bindingSms(@Qualifier("smsQueue") Queue smsQueue,
                              @Qualifier("smsExchange") FanoutExchange smsExchange) {
        return BindingBuilder.bind(smsQueue).to(smsExchange);
    }

    //    @RabbitListener(bindings = @QueueBinding(value = @org.springframework.amqp.rabbit.annotation.Queue("smsQueue"),
//            exchange = @org.springframework.amqp.rabbit.annotation.Exchange(value = "smsExchange", type = ExchangeTypes.FANOUT)), ackMode = "AUTO")
//    public void smsReceive(String msg) {
//        log.debug(msg);
//    }
    @RabbitListener(queues = "smsQueue", ackMode = "AUTO")
    @RabbitHandler
    public void smsReceive(String msg) {
        log.debug(msg);
    }
}
